package cool.lichuan.common.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author lichuan
 * @date 2021/11/20 15:01
 * @description 200成功 500失败 401认证失败 403没有权限  404不存在
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    /** 响应码 */
    private Integer code;
    /** 响应消息 */
    private String msg;
    /** 响应数据 */
    private T data;

    /** 操作成功响应---------------------*/
    public static <T>Result<T> success() {
        return Result.success("操作成功");
    }

    public static <T>Result<T> success(String msg) {
        return Result.success(msg, null);
    }

    public static <T>Result<T> success(T data) {
        return Result.success("操作成功", data);
    }

    public static <T>Result<T> success(String msg, T data) {
        return new Result<>(HttpStatus.OK.value(), msg, data);
    }
    /** 操作成功响应---------------------*/

    /** 操作失败响应---------------------*/
    public static <T>Result<T> error() {
        return Result.error("操作失败");
    }

    public static <T>Result<T> error(String msg) {
        return Result.error(msg, null);
    }

    public static <T>Result<T> error(String msg, T data) {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, data);
    }

    public static <T>Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }
    /** 操作失败响应---------------------*/

    private static final long serialVersionUID = 1487406640494799555L;
}
